package xhe.lib.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.binary.Base64;

public class Gzip {

	/**
	 * 压缩GZip
	 *
	 * @return String
	 */
	public static String gZip(String input) {
		byte[] bytes = null;
		GZIPOutputStream gzip = null;
		ByteArrayOutputStream bos = null;
		try {
			bos = new ByteArrayOutputStream();
			gzip = new GZIPOutputStream(bos);
			gzip.write(input.getBytes(CharEncoding.UTF_8));
			gzip.finish();
			gzip.close();
			bytes = bos.toByteArray();
			bos.close();
		} catch (Exception e) {
			// log.error("压缩出错：", e);
		} finally {
			try {
				if (gzip != null)
					gzip.close();
				if (bos != null)
					bos.close();
			} catch (final IOException ioe) {
				// log.error("压缩出错：", ioe);
			}
		}
		return Base64.encodeBase64String(bytes);
	}

	/**
	 * 解压GZip
	 *
	 * @return String
	 */
	public static String unGZip(String input) {
		byte[] bytes;
		String out = input;
		GZIPInputStream gzip = null;
		ByteArrayInputStream bis;
		ByteArrayOutputStream bos = null;
		try {
			bis = new ByteArrayInputStream(Base64.decodeBase64(input));
			gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[1024];
			int num;
			bos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				bos.write(buf, 0, num);
			}
			bytes = bos.toByteArray();
			out = new String(bytes, CharEncoding.UTF_8);
			gzip.close();
			bis.close();
			bos.flush();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (gzip != null)
					gzip.close();
				if (bos != null)
					bos.close();
			} catch (final IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return out;
	}

	/**
	 * <b> 方法: </b> 使用gzip进行压缩
	 * 
	 * @author xhe
	 * @param primStr 字符串
	 * @return String 压缩后的字符串
	 * @version V1.0.0.0
	 * @version --2021年9月14日
	 * @version --新增方法
	 */
	public static String compress(String primStr) {
		if (primStr == null || primStr.length() == 0) {
			return primStr;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = null;
		try {
			gzip = new GZIPOutputStream(out);
			gzip.write(primStr.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return new sun.misc.BASE64Encoder().encode(out.toByteArray());
	}

	/**
	 * <b> 方法: </b> 使用gzip进行解压缩
	 * 
	 * @author xhe
	 * @param primStr 字符串
	 * @return String 解压后的字符串
	 * @version V1.0.0.0
	 * @version --2021年9月14日
	 * @version --新增方法
	 */
	public static String uncompress(String compressedStr) {
		if (compressedStr == null) {
			return null;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = null;
		GZIPInputStream ginzip = null;
		byte[] compressed = null;
		String decompressed = null;
		try {
			compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
			in = new ByteArrayInputStream(compressed);
			ginzip = new GZIPInputStream(in);

			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = ginzip.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ginzip != null) {
				try {
					ginzip.close();
				} catch (IOException e) {
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			try {
				out.close();
			} catch (IOException e) {
			}
		}
		return decompressed;
	}
}
